
$(function() {
    
    // removes trailing and leading white spaces
    String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, '');
    };
    
    // removes all white spaces
    String.prototype.trimAll = function(){
	return this.replace(/ /g, '');
    };
    
    // capitalize first letter of a word or phrase
    String.prototype.capitalize = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
    // capitalize all words in a phrase and remove all dashes and underscores
    String.prototype.cleanCapitalizeAll = function() {
        return this.replace(/[_-]/g,' ').replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
    };
    
    // capitalize all words in a phrase
    String.prototype.capitalizeAll = function() {
        return this.replace(/[_-]/g,' ').replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
    };
    
    // de-capitalize first letter of a word or phrase
    String.prototype.deCapitalize = function() {
        return this.charAt(0).toLowerCase() + this.slice(1);
    };
    
    // de-capitalize all words in a phrase
    String.prototype.deCapitalizeAll = function() {
        return this.replace(/[_-]/g,' ').replace(/(?:^|\s)\S/g, function(a) { return a.toLowerCase(); });
    };
    
    // convert spaces and underscores to dash
    String.prototype.toDash = function() {
        return this.cleanCapitalizeAll().trimAll().replace(/([A-Z])/g, function(capital) {
            return ' ' + capital.toLowerCase();
        }).trim().replace(/ /g, '-');
    };
    
    // convert spaces and dashes to underscore
    String.prototype.toUnderscore = function() {
        return this.cleanCapitalizeAll().trimAll().replace(/([A-Z])/g, function(capital) {
            return ' ' + capital.toLowerCase();
        }).trim().replace(/ /g, '_');
    };
    
    // convert spaces and dashes to underscore
    String.prototype.toSnake = function() {
        return this.cleanCapitalizeAll().trimAll().replace(/([A-Z])/g, function(capital) {
            return ' ' + capital.toLowerCase();
        }).trim().replace(/ /g, '_');
    };
    
    // convert string to pascal case
    String.prototype.toPascalCase = function() {
        return this.cleanCapitalizeAll().trimAll().replace(/([A-Z])/g, function(capital) {
            return capital;
        });
    };
    
    // convert string to camel case
    String.prototype.toCamelCase = function(){
        return this.toPascal().deCapitalize();
    };
    
});

/**
 * Convert singular to plural
 * @param {string} singular 
 * @returns {string} Returns plural form of the string
 */
function plural( singular ) {
    var lastLetter = singular.charAt( singular.length - 1 );
    if( lastLetter === 'y') {
        singular = singular.substring(0, singular.length - 1) + 'ies';
        return singular;
    }
    if( lastLetter === 's' || lastLetter === 'h' || lastLetter === 'x' ) {
        singular = singular + 'es';
        return singular;
    }
    return singular + 's';
}


/**
 * Convert a string to a certain case
 * @access public
 * @param string $str String to be converted
 * @param string $case Case from which the string will be converted from
 * @return string Return converted string
 */
function changeCase(str, changeCase) {
    changeCase = changeCase || 'readable';
//    return str.replace(/ /g, '_');
    
    switch(changeCase)
    {
        case 'human' :
            return str.cleanCapitalizeAll();
            break;
        case 'humanLower' :
            return str.cleanCapitalizeAll().toLowerCase();
            break;
        case 'humanUpper' :
            return str.cleanCapitalizeAll().toUpperCase();
            break;
        case 'snake' :
            return str.toUnderscore();
            break;
        case 'underscore' :
            return str.toUnderscore();
            break;
        case 'dash' :
            return str.toUnderscore();
            break;
        case 'camel' :
            return str.toCamelCase();
            break;
        case 'pascal' :
            return str.toPascalCase();
            break;
       default :
           return str.toUnderscore();
           break;
    }
}